<?php
/**
 * 商家收益
 * xionggan
 * Date: 2019/06/14
 */

namespace service;

use app\common\model\CoreOrderIncome;
use think\Db;
use think\Exception;
use think\facade\Log;
use service\pay\VirtualPay;
use service\OrderService;
use app\store\model\Store;
use app\common\model\StoreIncomeLogModel;

class StoreIncomeService
{

    public function __construct()
    {

    }

    /**
     * 商家增加收益
     * @param   string  $order_sn   订单号
     * @param   number  $store_id   商家id
     * @param   number  $type       收益类型，1自提订单，2服务订单，3线下购买单
     *
     * @return  bool
     */
    public function addStoreIncome($order_sn, $store_id, $type)
    {
        $store_model    = new Store();
        $pay_service    = new VirtualPay();
        $order_service  = new OrderService();
        $income_model   = new StoreIncomeLogModel();

        try {
            //模型属性方法自动转换成了单位为元
            $order_data = $order_service->getOneByOrderSn($order_sn);
            $amount     = $order_data['amount_pay'];
            //bcadd($order_data['amount_pay'], $order_data['gold_coin'], 2); 

            if (empty($order_data) || $amount <= 0) {
                Log::write("商家收益: 未找到商家用户或金额出错 order_sn:".$order_sn, "error");
                return false;                
            }

            $store_data = $store_model->get_store_by_id($order_data['store_id']);

            if (empty($store_data['user_id'])) {
                Log::write("商家收益: 未找到商家 store_id:".$store_id, "error");
                return false;                
            }

            if ($type == 1) {
                $oper_type  = 4;
                $remark     = '订单号: '.$order_sn.' 提货收益';
                if ($store_data['self_percent'] <= 0) {
                    $balance = 0;
                } else {
                    $balance = bcmul($amount, bcdiv($store_data['self_percent'], 100, 2), 2);
                }
            } elseif ($type == 2) {
                $oper_type  = 5;
                $remark     = '订单号: '.$order_sn.' 服务订单';
                if ($store_data['service_percent'] <= 0) {
                    $balance    = $amount;
                } else {
                    $percent    = bcsub(1, bcdiv($store_data['service_percent'], 100, 2), 2);
                    $balance    = bcmul($amount, $percent, 2);
                }
            } elseif ($type == 3) {
                $oper_type  = 6;
                $remark     = '订单号: '.$order_sn.' 优惠买单';
                $balance    = $amount;
            }

            if ($balance <= 0) {
                $balance = 0;
            }

            $res = $pay_service->public_update_user_virtual(
                $store_data['user_id'], '', 1, 1, $balance, 0, $oper_type, $order_data['order_sn'], $remark
            );

            if ($res < 0) {
                Log::write("商家收益: 增加商家账户余额失败-1", "error");
                return false;
            }

            $income_data = [
                'store_id'      => $order_data['store_id'],
                'user_id'       => $store_data['user_id'],
                'order_sn'      => $order_data['order_sn'],
                'total'         => $amount * 100,
                'amount'        => $balance * 100,
                'remark'        => $remark,
            ];

            if ($type == 1) {
                $income_model = $income_model->savePickAdd($income_data);
            } elseif ($type == 2) {
                $income_model = $income_model->saveOrderAdd($income_data);
            } elseif ($type == 3) {
                $income_model = $income_model->saveCheckAdd($income_data);
            }
        } catch (Exception $e) {
            Log::write("商家收益: 增加商家账户余额失败-2 ".$e->getMessage(), "error");
            return false;
        }
    }

    /**
     * 商家增加收益
     * @param string $order_sn
     * @param array $product_list
     * @return
     */
    public function addStoreIncome2($order_sn,$product_list = [])
    {
        $pay_service    = new VirtualPay();
        $income_model   = new CoreOrderIncome();

        $where = ['order_sn'=>$order_sn,'product_id'=>$product_list];
        $income_data = $income_model->getIncomeData($where);
        foreach ($income_data as $v) {
            if ($v['type'] == CoreOrderIncome::TYPE_1) {
                $oper_type = 4;
            } elseif ($v['type'] == CoreOrderIncome::TYPE_2) {
                $oper_type = 8;
            } else {
                $oper_type = 9;
            }
            $remark = $v['product_name'].'自提收益';
            $pay_service->public_update_user_virtual(
                $v['user_id'], '', 1, 1, bcdiv($v['income'],100,2), 0, $oper_type, $v['income_id'], $remark
            );
            $income_model->updateIncomeFinish($v['income_id']);
        }
        return true;
    }

}
